<?php
/**
 * Created by PhpStorm.
 * User: bin
 * Date: 2022/9/13
 * Time: 11:53
 */
namespace Xin\Hyperf\Fortify\Model;
use Hyperf\Database\Model\Builder;
use Hyperf\Stringable\Str;

/**
 * @method $this|Builder search(array $data)
 */
trait Search
{
    /**
     * @var string[]
     */
    protected array $likeFields = [];

    /**
     * @return array
     */
    public static function getSearchFields(): array
    {
        return [];
    }

    /**
     * 搜索作用域
     * @param Builder $query
     * @param array $data
     */
    public function scopeSearch(Builder $query, array $data): void
    {
        foreach (static::getSearchFields() as $key => $field) {
            if (strpos('_',$field)) {
                $field($this, $data[$key] ?? null, $data);
            } else {
                $value = $data[$field] ?? null;
                // 检测搜索器
                $fieldName = is_numeric($key) ? $field : $key;
                $method = 'search' . Str::camel($field) . 'Attribute';
                if (method_exists($this, $method)) {
                    $this->$method($query, $value, $data);
                } elseif ($value !== null) {
                    if (in_array($fieldName, $this->likeFields, true)) {
                        $query->where($key, 'like', "%{$value}%");
                    } else {
                        $query->where($fieldName, $value);
                    }
                }
            }
        }
    }

    /**
     * @param Builder $query
     * @param mixed $value
     * @return void
     */
    public function searchKeywordsAttribute(Builder $query, mixed $value): void
    {
        $query->where('title', 'like', "%{$value}%");
    }
}
